package com.school.graduation.mine.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.model.Order;
import com.school.graduation.util.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class MyOrderActivity extends BaseActivity {

    private static final int CANCEL_ORDER = 0;
    private static final int FINISH_ORDER = 1;

    @Bind(R.id.order_toolBar)
    Toolbar orderToolBar;
    @Bind(R.id.order_condition)
    TextView orderCondition;
    @Bind(R.id.order_seller_name_up)
    TextView orderSellerNameUp;
    @Bind(R.id.order_contact_seller)
    RelativeLayout orderContactSeller;
    @Bind(R.id.order_good_img)
    ImageView orderGoodImg;
    @Bind(R.id.order_good_title)
    TextView orderGoodTitle;
    @Bind(R.id.order_good_price)
    TextView orderGoodPrice;
    @Bind(R.id.order_good_freight)
    TextView orderGoodFreight;
    @Bind(R.id.order_good)
    RelativeLayout orderGood;
    @Bind(R.id.order_buyer_name)
    TextView orderBuyerName;
    @Bind(R.id.order_buyer_phone)
    TextView orderBuyerPhone;
    @Bind(R.id.order_buyer_address)
    TextView orderBuyerAddress;
    @Bind(R.id.order_buyer)
    RelativeLayout orderBuyer;
    @Bind(R.id.order_seller_name)
    TextView orderSellerName;
    @Bind(R.id.order_seller_phone)
    TextView orderSellerPhone;
    @Bind(R.id.order_seller_address)
    TextView orderSellerAddress;
    @Bind(R.id.order_cancle)
    Button orderCancle;
    @Bind(R.id.order_finish)
    Button orderFinish;

    private Order order;

    public static void jumpToMyOrder(Context context, Bundle bundle) {
        Intent intent = new Intent(context, MyOrderActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_order_mine;
    }

    @Override
    public void initData() {
        initTitle();
        Intent intent = getIntent();
        order = (Order) intent.getSerializableExtra("order");
        Glide.with(context).load(order.getGoods().getPictures().get(0)).into(orderGoodImg);
        orderGoodTitle.setText(order.getGoods().getGoodsTitle());
        setCondition(order.getCondition(),orderCondition);
        orderSellerNameUp.setText(order.getGoods().getStudentIssue().getUsername());
        orderGoodPrice.setText(order.getGoods().getPrimeCurrent()+"");
        orderBuyerName.setText(order.getStudentOrder().getUsername());
        orderBuyerPhone.setText(order.getStudentOrder().getMobilePhoneNumber()+"");
        orderBuyerAddress.setText(order.getStudentOrder().getAddress());
        orderSellerName.setText(order.getGoods().getStudentIssue().getUsername());
        orderSellerPhone.setText(order.getGoods().getStudentIssue().getMobilePhoneNumber());
        orderSellerAddress.setText(order.getObjectId());
    }

    private void setCondition(int condition,TextView view) {
        switch (condition){
            case Order.COMMIT_ORDER:
                view.setText(Order.COMMIT_ORDER_STR);
                break;
            case Order.CANCEL_ORDER_SELLER:
                view.setText(Order.CANCEL_ORDER_SELLERSTR);
                orderCancle.setClickable(false);
                orderCancle.setText("已取消");
                orderFinish.setText("已取消");
                orderFinish.setClickable(false);
                orderFinish.setBackgroundColor(getResources().getColor(R.color.md_grey_400));
                break;
            case Order.CANCEL_ORDER_BUYER:
                view.setText(Order.CANCEL_ORDER_BUYER_STR);
                orderCancle.setClickable(false);
                orderFinish.setClickable(false);
                orderCancle.setText("已取消");
                orderFinish.setText("已取消");
                orderFinish.setBackgroundColor(getResources().getColor(R.color.md_grey_400));
                break;
            case Order.BUYYER_PAY:
                view.setText(Order.BUYYER_PAY_STR);
                setFinishBtText(condition);
                break;
            case Order.RECEIVE_MONEY:
                view.setText(Order.RECEIVE_MONEY_STR);
                setFinishBtText(condition);
                break;
            case Order.DEAL_FINISH:
                view.setText(Order.DEAL_FINISH_STR);
                setFinishBtText(condition);
                break;
        }
    }

    private void setFinishBtText(int condition) {
        switch (condition){
            case Order.BUYYER_PAY:
                if (order.getStudentOrder().getObjectId().equals(BmobUser.getCurrentUser().getObjectId())){
                    orderFinish.setClickable(false);
                    orderCancle.setClickable(false);
                    orderCancle.setText("已无法取消");
                    orderFinish.setText("已确认支付");
                    return;
                }
                if (order.getGoods().getStudentIssue().getObjectId().equals(BmobUser.getCurrentUser().getObjectId())){
                    orderCancle.setClickable(false);
                    orderCancle.setText("已无法取消");
                    orderFinish.setText("等待确认");
                }
                break;
            case Order.RECEIVE_MONEY:
            case Order.DEAL_FINISH:
                orderCancle.setClickable(false);
                orderCancle.setText("已无法取消");
                orderFinish.setText("已完成");
                orderFinish.setClickable(false);
                break;
        }
    }

    private void initTitle() {
        orderToolBar.setTitle("订单详情");
        orderToolBar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    }

    @OnClick({R.id.order_contact_seller, R.id.order_good, R.id.order_cancle, R.id.order_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_contact_seller:

                break;
            case R.id.order_good:
                break;
            case R.id.order_cancle:
                new MaterialDialog.Builder(context)
                        .content("单方面取消订单会对您的信用度产生影响，确认取消订单？")
                        .negativeText("取消")
                        .positiveText("确认")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                modifyOrderCondition(CANCEL_ORDER);
                            }
                        }).build().show();
                break;
            case R.id.order_finish:
                new MaterialDialog.Builder(context)
                        .content("单方面完成交易会对您的信用度产生影响，确认完成交易？")
                        .negativeText("取消")
                        .positiveText("确认")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                modifyOrderCondition(FINISH_ORDER);
                            }
                        }).build().show();
                break;
        }
    }

    private void modifyOrderCondition(int operation) {
        switch (operation){
            case CANCEL_ORDER:
                final Order newOrder = new Order();
                if (order.getGoods().getStudentIssue().getObjectId().equals(BmobUser.getCurrentUser().getObjectId()))
                    newOrder.setCondition(Order.CANCEL_ORDER_SELLER);
                else if (order.getStudentOrder().getObjectId().equals(BmobUser.getCurrentUser().getObjectId()))
                    newOrder.setCondition(Order.CANCEL_ORDER_BUYER);
                newOrder.update(order.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null) {
                            setCondition(newOrder.getCondition(),orderCondition);
                            finish();
                        }
                        else ToastUtil.makeShortToast(context,"更新订单状态失败"+e);
                    }
                });
                break;
            case FINISH_ORDER:
                final Order newOrder1 = new Order();
                if (order.getGoods().getStudentIssue().getObjectId().equals(BmobUser.getCurrentUser().getObjectId()))
                    newOrder1.setCondition(Order.RECEIVE_MONEY);
                else if (order.getStudentOrder().getObjectId().equals(BmobUser.getCurrentUser().getObjectId()))
                    newOrder1.setCondition(Order.BUYYER_PAY);
                newOrder1.update(order.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null) {
                            setCondition(newOrder1.getCondition(),orderCondition);
                            finish();
                        }
                        else ToastUtil.makeShortToast(context,"更新订单状态失败"+e);
                    }
                });
                break;
        }
    }
}
